package com.foreveross.testproxy.timer;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.support.logging.Log;
import com.foreveross.constant.Constants;
import com.foreveross.model.ProxyIpModel;
import com.foreveross.util.QueueUtil;
import com.foreveross.util.cfg.defaults.DefaultPropertiesLoader;
import com.foreveross.util.redis.JRedisImpl;
import com.foreveross.util.redis.RedisService;


public class RedisTianxunTimer {
	private static final Logger LOG = LoggerFactory.getLogger(RedisTianxunTimer.class);
	private Queue<Object> q ;
	int startTime=0;
	int period =0;

	private RedisService redisServiece = new JRedisImpl();
	
	public RedisTianxunTimer(Queue q){
		this.q = q;
		startTime = Integer.parseInt(DefaultPropertiesLoader.getTimerTianxunCheckIpStartTime());
		period = Integer.parseInt(DefaultPropertiesLoader.getTimerTianxunCheckIpTime());

		 k();
	}
	public void k() {
		TimerTask task = new TimerTask() {
			public void run() {
				job();
			}
		};
		task.run();
		
		Timer timer = new Timer();
		// 第一参数 从第几秒开始  第二参数 表示 每隔多少秒执行一个任务
		timer.schedule(task, startTime, period);
	}
	
	public void job() {
		List <ProxyIpModel>  proxyList = new ArrayList();
		int size = q.size()>20?20:q.size();
		for(int i=0;i<size;i++){
			ProxyIpModel proxy = (ProxyIpModel) QueueUtil.pollQueue(q);
			proxyList.add(proxy);
		}

		boolean poolFlag = redisServiece.persistenceProxyIP(
				Constants.CHANNEL_TIANXUN, proxyList);
		LOG.error("成功提交若干个IP到热池 大小"+size);

	}
	
}
